package com.wuseguang.springcloud.dao.jdbc;

import com.wuseguang.springcloud.JdbcTemplate.BaseTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;

/**
 * @描述
 * @创建人 zm
 * @创建时间 2020/8/3 12:34
 */
@Repository
public class AttendTwoJdbc extends BaseTemplate {
    private Logger logger = LoggerFactory.getLogger(this.getClass());


    public int updateAttendGroupUser(String listSql, String userids, Integer id) {
        try {
            logger.info("updateAttendGroupUser,userids={},groupid={}", userids, id);
            String sql = " DELETE FROM attend_group_user WHERE attend_group_id=? ";
            updateByParameter(sql, id);
            String[] split = userids.split(",");
            return dao.batchUpdate(listSql, new BatchPreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    String s = split[i];
                    ps.setInt(1, Integer.parseInt(s));
                    ps.setInt(2, id);
                }

                @Override
                public int getBatchSize() {
                    return split.length;
                }
            }).length;
        } catch (Exception e) {
            logger.info("批量添加异常,error={}", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();// 手动回滚
        }
        return 0;
    }


    public int updateAttendGroupClass(String listSql, String classids, Integer id) {
        try {
            logger.info("updateAttendGroupClass,classids={},groupid={}", classids, id);
            String sql = " DELETE FROM class_attend_group WHERE attend_group_id=? ";
            updateByParameter(sql, id);
            String[] split = classids.split(",");
            return dao.batchUpdate(listSql, new BatchPreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    String s = split[i];
                    ps.setInt(1, Integer.parseInt(s));
                    ps.setInt(2, id);
                }

                @Override
                public int getBatchSize() {
                    return split.length;
                }
            }).length;
        } catch (Exception e) {
            logger.info("批量添加异常,error={}", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();// 手动回滚
        }
        return 0;
    }



    public int updateAttendGroupShift(String listSql, String shift, Integer id) {
        try {
            logger.info("updateAttendGroupShift,classids={},groupid={}", shift, id);
            String sql = " DELETE FROM attend_group_shift WHERE attend_group_id=? ";
            updateByParameter(sql, id);
            String[] split = shift.split(",");
            return dao.batchUpdate(listSql, new BatchPreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    String s = split[i];
                    ps.setInt(1, Integer.parseInt(s));
                    ps.setInt(2, id);
                    ps.setInt(3, i);
                }

                @Override
                public int getBatchSize() {
                    return split.length;
                }
            }).length;
        } catch (Exception e) {
            logger.info("批量添加异常,error={}", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();// 手动回滚
        }
        return 0;
    }


    public Map<String, String> queryAttendListMap(String sql, Object... args) {
        logger.info("AttendJdbc==>queryAttendListMap,sql={}", sql);
        return dao.query(sql, args, rs -> {
            Map<String, String> map = new HashMap<String, String>();
            while (rs.next()) {
                String userid = rs.getString("userid");
                String attend = rs.getString("attend");
                map.put(userid, attend);
            }
            return map;
        });
    }

}
